package com.treasure.hunt.app

import android.app.ActivityManager
import android.content.Context
import android.os.Process
import androidx.multidex.MultiDex
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.fm.openinstall.OpenInstall
import com.kwai.monitor.log.TurboAgent
import com.kwai.monitor.log.TurboConfig
import com.module.lemlin.base.BaseApplication
import com.qq.e.comm.managers.GDTADManager
import com.treasure.hunt.api.subscribe.HttpServiceFactory
import com.treasure.hunt.config.TTAdManagerHolder
import com.treasure.hunt.entity.UserInfo
import com.umeng.analytics.AnalyticsConfig
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure
import rxhttp.RxHttp

class THApplication : BaseApplication() {

    companion object {
        private const val KEY_HTTP_TOKEN = "token"
        private const val KEY_HTTP_CHANNEL = "channel"
        private const val KEY_HTTP_VERSION = "version"

        var userInfo: UserInfo? = null
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(base)
    }

    override fun onCreate() {
        super.onCreate()

        //设置http debug模式
        RxHttp.setDebug(true)
        //初始化
//        RxHttp.init(HttpServiceFactory.client().build())
        //设置http公共请求头token
        RxHttp.setOnParamAssembly {
            val token: String? = SPStaticUtils.getString("token", null)
            if (!token.isNullOrEmpty()) {
                it.addHeader(KEY_HTTP_TOKEN, token)
            }
            it.addHeader(KEY_HTTP_VERSION, AppUtils.getAppVersionName())
            it.addHeader(KEY_HTTP_CHANNEL, AnalyticsConfig.getChannel(this))
            return@setOnParamAssembly it
        }

        //设置友盟log开关
        //UMConfigure.setLogEnabled(true)
        //初始化友盟SDK
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "")
        //选用AUTO页面采集模式
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO)

        //穿山甲SDK初始化
        //TTAdManagerHolder.init(INSTANCE)

        //OpenInstall
        OpenInstall.init(INSTANCE)

        //优量汇SDK初始化
        //GDTADManager.getInstance().initWith(INSTANCE, AppConstance.APP_ID_YLH)

        //快手SDK初始化
        if (packageName == getProcessName(this)) {
            TurboAgent.init(
                TurboConfig.TurboConfigBuilder.create(this)
                    .setAppId("70193")
                    .setAppName("zhijianxunbao")
                    //.setAppChannel(AnalyticsConfig.getChannel(this))
                    .setEnableDebug(true)
                    .build()
            )
        }

    }

    private fun getProcessName(context: Context): String? {
        var sCurProcessName: String? = null
        try {
            val pid = Process.myPid()
            val mActivityManager =
                context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            for (appProcess in mActivityManager.runningAppProcesses) {
                if (appProcess.pid == pid) {
                    sCurProcessName = appProcess.processName
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return sCurProcessName
    }

}